<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="真实姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="微信昵称" prop="userNickname">
        <el-input
          v-model="queryParams.userNickname"
          placeholder="请输入用户微信昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="队伍名称" prop="teamName">
        <el-select v-model="queryParams.teamId" filterable placeholder="请选择队伍名称" clearable>
          <el-option
            v-for="dict in teamList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning"  icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['applet:weChatUser:export']">导出</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['applet:weChatUser:export']">导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="weChatUserList" border>
      <el-table-column label="主键" align="center" prop="userId" v-if="false"/>
      <el-table-column label="微信昵称" width="150" align="center" prop="userNickname"/>
      <el-table-column label="真实姓名" width="150" show-overflow-tooltip align="center" prop="userName"/>
      <el-table-column label="用户头像" align="center" prop="userImg" width="100">
        <template slot-scope="scope">
          <el-avatar :size="50" :src="scope.row.userImg"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" width="150" show-overflow-tooltip align="center" prop="userPhone"/>
      <el-table-column label="队伍名称" width="150" show-overflow-tooltip align="center" prop="teamName"/>
      <el-table-column label="累计里程" width="150" align="center" prop="mileage">
        <template slot-scope="scope">
          {{ scope.row.mileage }}
        </template>
      </el-table-column>
      <el-table-column label="累计天数" width="150" align="center" prop="daySum">
        <template slot-scope="scope">
          {{ scope.row.daySum }} 天
        </template>
      </el-table-column>
      <el-table-column label="最后登录时间" align="center" prop="lastLoginTime" width="180"/>
      <el-table-column label="注册时间" align="center" prop="registerTime" width="160"/>
      <el-table-column label="小程序唯一标识" width="180" show-overflow-tooltip align="center" prop="openid"/>
      <el-table-column label="备注" width="200" show-overflow-tooltip align="center" prop="remark"/>
      <el-table-column label="操作" width="200" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:weChatUser:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:weChatUser:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改weChatUser对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :sm="24" :lg="12">
            <el-form-item label="微信昵称：" prop="userNickname">
              <el-input v-model="form.userNickname" placeholder="请输入用户微信昵称"/>
            </el-form-item>
            <el-form-item label="真实姓名：" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入姓名"/>
            </el-form-item>
            <el-form-item label="手机号码：" prop="userPhone">
              <el-input v-model="form.userPhone" placeholder="请输入手机号码"/>
            </el-form-item>
            <el-form-item label="小程序标识：" prop="openid">
              <el-input v-model="form.openid" disabled placeholder="请输入小程序唯一标识"/>
            </el-form-item>

          </el-col>
          <el-col :sm="24" :lg="12">
            <el-form-item label="用户头像：" prop="userImg">
              <image-upload v-model="form.userImg" :limit="1"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :sm="24" :lg="8">
            <el-form-item label="所属队伍" prop="teamId">
              <el-select v-model="form.teamId" filterable placeholder="请选择队伍名称" clearable>
                <el-option
                  v-for="dict in teamList"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :lg="8">
            <el-form-item label="累计里程：" prop="mileage">
              <el-input-number :precision="2" :min="0"  v-model="form.mileage" placeholder="请输入累计里程"/>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :lg="8">
            <el-form-item label="累计天数：" prop="daySum">
              <el-input-number :min="1"  v-model="form.daySum" placeholder="请输入累计兑换天数"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea"
                    :autosize="{ minRows: 2, maxRows: 4}" v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listWeChatUser, getWeChatUser, delWeChatUser, addWeChatUser, updateWeChatUser} from "@/api/applet/weChatUser";
import {getTeamSelect} from "@/api/applet/appletTeam";

export default {
  name: "WeChatUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // weChatUser表格数据
      weChatUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        userPhone: null,
        userNational: null,
        registerTime: null,
        userIntegral: null,
        userImg: null,
        userNickname: null,
        lastLoginTime: null,
        mileage: null,
        daySum: null,
        openid: null,
        teamId: null,
        teamName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          {required: true, message: "姓名不能为空", trigger: "blur"}
        ],
        userPhone: [
          {required: true, message: "手机号码不能为空", trigger: "blur"}
        ],
        userNational: [
          {required: true, message: "民族不能为空", trigger: "blur"}
        ],
        registerTime: [
          {required: true, message: "注册时间不能为空", trigger: "blur"}
        ],
        userIntegral: [
          {required: true, message: "用户积分不能为空", trigger: "blur"}
        ],
        userImg: [
          {required: true, message: "用户头像不能为空", trigger: "blur"}
        ],
        userNickname: [
          {required: true, message: "用户昵称不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ],
        updateBy: [
          {required: true, message: "更新账户不能为空", trigger: "blur"}
        ],
        lastLoginTime: [
          {required: true, message: "最后登录时间不能为空", trigger: "blur"}
        ],
        mileage: [
          {required: true, message: "累计里程不能为空", trigger: "blur"}
        ],
        daySum: [
          {required: true, message: "累计兑换天数不能为空", trigger: "blur"}
        ],
        openid: [
          {required: true, message: "小程序唯一标识不能为空", trigger: "blur"}
        ],
        teamId: [
          {required: true, message: "队伍ID不能为空", trigger: "blur"}
        ],
        teamName: [
          {required: true, message: "队伍名称不能为空", trigger: "blur"}
        ]
      },
      teamList: []
    };
  },
  created() {
    this.getList();
    this.getTeamList()
  },
  methods: {
    //获取路线坐标图
    getTeamList() {
      getTeamSelect().then(res => {
        this.teamList = res.data;
      })
    },
    /** 查询weChatUser列表 */
    getList() {
      this.loading = true;
      listWeChatUser(this.queryParams).then(response => {
        this.weChatUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        userName: null,
        userPhone: null,
        userNational: null,
        registerTime: null,
        userIntegral: null,
        userImg: null,
        userNickname: null,
        updateTime: null,
        updateBy: null,
        lastLoginTime: null,
        mileage: null,
        daySum: null,
        openid: null,
        createTime: null,
        createBy: null,
        remark: null,
        teamId: null,
        teamName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加weChatUser";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getWeChatUser(userId).then(response => {
        this.form = response.data;
        if (this.form.teamId != null){
          this.form.teamId = this.form.teamId.toString()
        }
        this.open = true;
        this.title = "修改小程序用户：" + row.userNickname;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateWeChatUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWeChatUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除weChatUser编号为"' + userIds + '"的数据项？').then(function () {
        return delWeChatUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/weChatUser/export', {
        ...this.queryParams
      }, `weChatUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
